#include <stdio.h>
#include <stdlib.h>
#include <sys/resource.h>
#include "aimage.h"
#include "aimage_utils.h"
#include "aimage_ppm.h"

int main( int argc, const char *argv[] )
{
	AIMAGE image;
	ASTAR_FIELD orig_stars;
	int i;
	AIMAGE orig_image;
	int width, height;
	ASTAR_TRANSFORM t;

	const rlim_t kStackSize = 64L * 1024L * 1024L;
	struct rlimit rl;

	int result;
	result = getrlimit( RLIMIT_STACK, &rl );
	if ( result == 0 )
	{
		fprintf( stderr, "Stack size is %dK\n", ((int) ( rl.rlim_cur / 1024L )));
		if ( rl.rlim_cur < kStackSize )
		{
			rl.rlim_cur = kStackSize;
			result = setrlimit( RLIMIT_STACK, &rl );
			if ( result == 0 )
			{
				fprintf( stderr, "Stack size set to %dK\n", ((int) (rl.rlim_cur / 1024L )));
			}
		}
	}

	setrlimit( RLIMIT_STACK, &rl );

	if ( argc <= 1 )
	{
		fprintf(stderr, "%s file1 file2...\n", argv[0] );
		exit( 1 );
	}

	for ( i = 1; i < argc; ++i )
	{
		srand( 1 );
		AIMAGE_PPM_UTILS::load( image, argv[i] );
		image.get_dim( width, height );

		orig_image.alloc_image( width, height );
		AIU::cls( orig_image, FULL_INT, 0, 0 );
		orig_image.copy( image, 0, 0, width, height, 0, 0 );

		orig_image.makemask( .1 );

		float x,y, r, error;

		x = 2500;
		y = 1500;
		r = 1517;

		orig_image.circlefind( x, y, r, error );
		//fprintf(stderr, "%s X %g Y %g R %g Error %g\n", argv[ i ], x, y, r, error);
		AIMAGE_PPM_UTILS::save( orig_image, "stdout", false );
		//printf("%s X %g Y %g R %g Error %g\n", argv[ i ], x, y, r, error);
//		//return 0;
	}

#ifdef NOPE
#endif

	//orig_image.writeppm( "stdout", false );
	//image.writeppm( "stdout", false );

}

